package com.dupt.system.service.impl;

import com.dupt.common.utils.DictUtils;
import com.dupt.system.domain.MaterialBorrowReturn;
import com.dupt.system.domain.vo.MaterialDingTalkVo;
import com.dupt.system.mapper.MaterialBorrowReturnMapper;
import com.dupt.system.mapper.MaterialDetialMapper;
import com.dupt.system.mapper.MaterialDingTalkMapper;
import com.dupt.system.service.IMaterialDingTalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物料钉钉Service业务层处理
 *
 * @author zhuanyu.sun
 * @date 2025-06-10
 */
@Service
public class MaterialDingTalkServiceImpl implements IMaterialDingTalkService {

    @Autowired
    private MaterialDingTalkMapper materialDingTalkMapper;
    @Autowired
    private MaterialDetialMapper materialDetialMapper;
    @Autowired
    private MaterialBorrowReturnMapper materialBorrowReturnMapper;

    @Override
    public String selectMaterialDetialByborrowerId(Long userId) {
        return "";
    }

    @Override
    public int borrowAndReturnByInnerCode(String innerCode) {
        return 0;
    }

    /**
     * 根据内码、序列号查询物料
     * @param materialInnerCode
     * @param serialNo
     * @return
     */
    @Override
    public MaterialDingTalkVo selectMaterialDingTalkByInnerCodeAndSerialNo(String materialInnerCode,String serialNo) {
        MaterialDingTalkVo vo = materialDingTalkMapper.selectMaterialDingTalkByInnerCodeAndSerialNo(materialInnerCode,serialNo);
        vo.setApplicableTemp(DictUtils.getDictLabel("sys_applicable_temp",vo.getApplicableTemp()));
        vo.setStatus(DictUtils.getDictLabel("sys_material_status",vo.getStatus()));
        return vo;
    }

    /**
     * 新增借用归还信息
     *
     * @param materialBorrowReturn 借用归还信息
     * @return 结果
     */
    @Override
    public int insertMaterialBorrowReturn(MaterialBorrowReturn materialBorrowReturn) {
        return materialBorrowReturnMapper.insertMaterialBorrowReturn(materialBorrowReturn);
    }

    /**
     * 查询当前使用者名下的物料
     *
     * @param userId 用户id
     * @return 列表
     */
    @Override
    public List<MaterialDingTalkVo> selectMaterialDingTalkByborrowerId(String userId) {
        List<MaterialDingTalkVo> list = materialDingTalkMapper.selectMaterialDingTalkByborrowerId(userId);
        list.forEach(detail -> {
            detail.setStatus(DictUtils.getDictLabel("sys_material_status",detail.getStatus())); // 设置为"在库"
            detail.setApplicableTemp(DictUtils.getDictLabel("sys_applicable_temp",detail.getApplicableTemp()));
        });
        return list;
    }

    @Override
    public MaterialDingTalkVo selectMaterialDingTalkBySerialNo(String serialNo) {
        MaterialDingTalkVo vo = materialDingTalkMapper.selectMaterialDingTalkBySerialNo(serialNo);
        vo.setApplicableTemp(DictUtils.getDictLabel("sys_applicable_temp",vo.getApplicableTemp()));
        vo.setStatus(DictUtils.getDictLabel("sys_material_status",vo.getStatus()));
        return vo;
    }
}
